﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using Safe_Driving.TokenUser;

namespace Safe_Driving
{
    //Class partiel MainWindow gérant tous les controles de SelectClientTabItem
    public partial class MainWindow
    {
        #region methods window

            //populate the listBox_selectClient list
            public void populatelistBox_selectClient() 
            {
                AppSafeDriving dbApp = SingletonTokenUser.GetInstance().DbApp;
                
                IEnumerable<SFClient> q = (  from sfClient in dbApp.SFClient
                                                select sfClient);
                dataGrid_selectClient.ItemsSource = q;
            }

        #endregion

        #region evt
        
            //Evt textboxchanged
            private void textBox_rechercheClient_TextChanged(object sender, TextChangedEventArgs e)
            {
                test_chercheBtnEnable();
            }

            //Evt comboBox_rechercheClient
            private void comboBox_rechercheClient_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                test_chercheBtnEnable();
            }

            //passe btn_chercheClient enable en tue ou false
            private void test_chercheBtnEnable()
            {
                if(textBox_rechercheClient.Text != "")
                {
                    btn_rechercheClient.IsEnabled = true;
                }
                else
                {
                    btn_rechercheClient.IsEnabled = false;
                }
            }

            //Evt datagrid selectClient
            private void dataGrid_selectClient_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                if (dataGrid_selectClient.SelectedItem != null)
                {
                    btn_selectClient.IsEnabled = true;
                }
                else
                {
                    btn_selectClient.IsEnabled = false;
                }
            }

            //Evt textbox key down rechercheClient
            private void textBox_rechercheClient_KeyUp(object sender, KeyEventArgs e)
            {
                if (e.Key == Key.Enter)
                {
                    btn_rechercheClient_Click(sender, e);
                }
            }
        
            //Evt bouton selectClient
            private void btn_selectClient_Click(object sender, RoutedEventArgs e)
            {
                SingletonTokenUser tok = SingletonTokenUser.GetInstance();

                SFClient client = (SFClient) dataGrid_selectClient.SelectedItem;
                tok.Client_selected = client;

                //Mise à jour du bandeau
                RibbonLbl_ClientNom.Content = client.NoM;
                RibbonLbl_ClientPrenom.Content = client.PreNoM;
                RibbonLbl_ClientLogin.Content = client.Login;

                Action_Client_Manager.IsEnabled = true;
                Client_Selection_Manager.IsEnabled = true;

                goToTab(Suivis_Client_TabItem);
                initClient();
            }

            //Evt bouton rechercheClient
            private void btn_rechercheClient_Click(object sender, RoutedEventArgs e)
            {
                dataGrid_selectClient.ItemsSource = null;
                AppSafeDriving dbApp = SingletonTokenUser.GetInstance().DbApp;
                IEnumerable<SFClient> q;
                
                string test = comboBox_rechercheClient.Text;
                switch (test)
                {
                    case "Login":
                        q = (from sfClient in dbApp.SFClient
                             where sfClient.Login == (textBox_rechercheClient.Text.ToString())   
                             select sfClient);
                        break;

                    case "Nom":
                        q = (from sfClient in dbApp.SFClient
                             where sfClient.NoM == (textBox_rechercheClient.Text.ToString())
                             select sfClient);
                        break;

                    case "Prénom":
                        q = (from sfClient in dbApp.SFClient
                             where sfClient.PreNoM == (textBox_rechercheClient.Text.ToString())
                             select sfClient);
                        break;

                    case "Agence":
                        q = (from sfClient in dbApp.SFClient
                             where sfClient.IdaGenCe == int.Parse(textBox_rechercheClient.Text.ToString())
                             select sfClient);
                        break;

                    case "Ville":
                        q = (from sfClient in dbApp.SFClient
                             where sfClient.VIllE == (textBox_rechercheClient.Text.ToString())
                             select sfClient);
                        break;

                    case "Email":
                        q = (from sfClient in dbApp.SFClient
                             where sfClient.Email == (textBox_rechercheClient.Text.ToString())
                             select sfClient);
                        break;

                    default:
                        q = (from sfClient in dbApp.SFClient
                             select sfClient);
                        break;
                }
                
                dataGrid_selectClient.ItemsSource = q;
            }

        #endregion
    }
}
